$(function () {
    // var giteeId = 'zzm_567';
    // var redirectUri = window.location.origin + '/cla';
    // var clienId = '';
    var _hmt = _hmt || [];
    (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?ab8d86daab9a8e98cf8faa239aefcd3c";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
    })();
    var remoteMethods = {
        /**
         * 获取统计数据
         * cb 回调函数
         */
        getData: function (id, cb) {
            $.ajax({
                type: 'GET',
                url: '/blessapi/detail',
                data: {
                    type: 'openeuler',
                    user: id
                },
                contentType: 'application/json; charset=utf-8',
                crossDomain: true,
                datatype: 'json',
                success: function (data) {
                    cb && cb(data);
                },
                error: function (data) {
                    alert('net error');
                }
            });
        },
        oauthLogin: function(cb) {
            $.ajax({
                type: 'GET',
                url: '/oauth2/userinfo',
                contentType: 'application/json; charset=utf-8',
                crossDomain: true,
                datatype: 'json',
                success: function (data) {
                    cb && cb(data);
                },
                error: function (data) {
                    $('body').removeClass('hide');
                    alert('net error');
                }
            });
        },
    }
    var localMethods = {
        /**
         * 页面初始化
         */
        init : function (){
            // giteeId = localMethods.readCookie('id');
            // if (!giteeId) {
            //     // localMethods.oauthLogin();
            //     return;
            // }
            remoteMethods.oauthLogin(function (userInfo) {
                remoteMethods.getData(userInfo.user, function (data) {
                    $('body').removeClass('hide');
                    if(data.list && data.list.length){
                        data = data.list[0];
                        $('.js-name').text(data.name);
                        if(localMethods.formatDate(data.first_time)){
                            $('.js-first-time').text(localMethods.formatDate(data.first_time));
                        }
                        if(data.days){
                            $('.js-computed-date').text(data.days + '天');
                        }
                        if(localMethods.formatDate(data.first_star_time)){
                            $('.js-first-star').text(localMethods.formatDate(data.first_star_time));
                        }else{
                            $('.js-first-star').parent().text('感谢这场相遇，让我们的故事有了开端...');
                        }
                        if(localMethods.formatDate(data.first_issue_time)){
                            $('.js-first-issue-time').text(localMethods.formatDate(data.first_issue_time));
                        }else{
                            $('.js-first-issue-time').parent().text('期待在2021年，收到你的第一个issue；');
                        }
                        if(localMethods.formatDate(data.first_pr_time)){
                            $('.js-first-pr-time').text(localMethods.formatDate(data.first_pr_time));
                        }else{
                            $('.js-first-pr-time').parent().text('期待在2021年，收到你的第一个pr；');
                        }
                        if(Number(data.pr)){
                            $('.js-pr').text(data.pr);
                        }else{
                            $('.js-pr').parent().addClass('hide');
                        }
                        if(Number(data.issue)){
                            $('.js-issue').text(data.issue);
                        }else{
                            $('.js-issue').parent().addClass('hide');
                        }
                        if(Number(data.total_comment)){
                            $('.js-comments').text(data.total_comment);
                        }else{
                            $('.js-comments').parent().addClass('hide');
                        }
                        if(Number(data.code_lines)){
                            $('.js-code-lines').text(data.code_lines);
                        }else{
                            $('.js-code-lines').parent().addClass('hide');
                        }
                        // if(Number(data.sigs)){
                        //     $('.js-sigs').text(data.sigs);
                        // }else{
                        //     $('.js-sigs').parent().addClass('hide');
                        // }
                        if(Number(data.stars)){
                            $('.js-stars').text(data.stars);
                        }else{
                            $('.js-stars').parent().addClass('hide');
                        }
                        if(data.defeat_value){
                            $('.js-defeat-value').text(data.defeat_value);
                            localMethods.checkDefeatText(data.defeat_value); 
                        }
                          
                    }else{
                        $('.pc-container').remove();
                        $('.h5-container').remove();
                        $('.no-user').removeClass('hide');
                        $('.no-user .wrap span').text(userInfo.preferredUsername || '');
                    }
                    
                });
            })
            
            $('.front').click(function () {
                $('.front').closest(".flip-container").addClass('click');
            })
        },
        readCookie: function(name) {
            var namePrefix = name + "=";
            var cookies = document.cookie.split(';');
            for(var i=0; i < cookies.length; i++) {
                var c = cookies[i];
                while (c.charAt(0)===' ') c = c.substring(1, c.length);
                if (c.indexOf(namePrefix) === 0) return c.substring(namePrefix.length, c.length);
            }
            return null;
        },
        formatDate: function (date) {
            if(!date){
                return '';
            }
            date = date.split(' ')[0].split('/');
            return date[0] + '年' + date[1] + '月' + date[2] + '日';
        },
        computedDate: function (dateTime) {
            // 指定日期和时间
            var EndTime = new Date(dateTime);
            // 当前系统时间
            var NowTime = new Date();
            var t = -(EndTime.getTime() - NowTime.getTime());
            var d = Math.floor(t / 1000 / 60 / 60 / 24);
            var h = Math.floor(t / 1000 / 60 / 60 % 24);
            // var m = Math.floor(t / 1000 / 60 % 60);
            // var s = Math.floor(t / 1000 % 60);
            var html = d + " 天" + h + " 小时";
            return html;
        },
        checkDefeatText: function (value) {
            var $textDom = $('.js-defeat-text');
            value = Number(value.split('.')[0]);
            if(value < 25){
                $textDom.html('<span class="top">默默思考的成员</span>，<br><span class="bottom">相信你会一鸣惊人!</span>').addClass('color4');
            }else if(value < 50){
                $textDom.html('<span class="top">稳步前进的行者</span>，<br><span class="bottom">你的贡献不可或缺!</span>').addClass('color3');
            }else if(value < 75){
                $textDom.html('<span class="top">一码当先的玩家</span>，<br><span class="bottom">请继续带我飞!</span>').addClass('color2');
            }else{
                $textDom.html('<span class="top">当之无愧的大佬</span>，<br><span class="bottom">请收下我的膝盖!</span>').addClass('color1');
            }
        }
    }
    

    var initFn = function () {
        localMethods.init();
    }



    initFn();
})